<?php

declare (strict_types=1);

namespace app\auto\controller;

use \think\admin\Controller;

class Field extends Controller {

    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'AutoField';
    
    public $type;

    public function initialize() {
        $this->title = '字段管理';
        $this->type = [
            'varchar(80)' => '字符串',
            'int(20)' => '数字',
            'tingint(1)' => '布尔',
            'decimal(20,2)' => '小数2位',
            'text' => '文本',
            'timestamp' => '时间戳',
            'varchar(200)' => '图片',
            'varchar(999)' => '图片组',
            'varchar(201)' => '文件',
            'varchar(1000)' => '文件组'
        ];
    }

    /**
     * 字段管理
     * @auth true
     * @menu true
     * @login true
     */
    public function index() {
        $this->_query($this->table)->where(['table_id' => input('table_id')])->order('sort desc,id asc')->page();
    }

    /**
     * 添加
     * @auth true
     */
    function add() {
        $this->_form($this->table, 'form');
    }
    
        /**
     * 批量添加字段
     * @auth true
     */
    function add_fields() {
        $this->_form($this->table, 'form_fields');
    }
        /**
     * 添加字段数据前置处理
     * @param array &$data
     */
    protected function _add_fields_form_filter(array &$data) {
                if($this->request->isPost()){
                    //获取列表字段名称数组
                    $fields= str2arr($data['title'], "\n");
                    foreach ($fields as $field){
                        
                        data_save($this->table, [
                            'table_id' => input('table_id'),
                            'title' => $field,
                            'name'  => '',
                            'type'  => 'varchar(80)',
                            'list'  => 1,
                            'search'    => 0
                        ]);
                    }
                            $this->success('批量添加完成');
                }
    }

    /**
     * 编辑
     * @auth true
     */
    function edit() {
        $this->_form($this->table, 'form');
    }

    /**
     * 删除
     * @auth true
     */
    public function remove() {
        $this->_delete($this->table);
    }

}